/*
 * @Description  : Vite 脚手架配置
 * @Autor        : 杜康
 * @Date         : 2023-03-07 15:07:13 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2024-11-05 10:04:25 +0800
 */
import { fileURLToPath, URL } from "node:url";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
// server https
import basicSsl from "@vitejs/plugin-basic-ssl";
import vueJsx from "@vitejs/plugin-vue-jsx";
import path from "path";
// 路径
const resolve = (dir) => path.resolve(__dirname, dir);

// 脚手架工具类
import { useEnv, proxy } from "./config/utils.js";

export default ({ command, mode }) => {
  // 参数mode为开放模式或生产模式
  // 获取.env文件里定义的环境变量
  const env = loadEnv(mode, `${process.cwd()}/env`);
  const { VITE_BASE, VITE_PROT, VITE_LOCAL_HTTPS, VITE_OPEN } = useEnv(env);
  // 默认导入插件
  const defaultPlugins = [vue(), vueJsx()];
  // 是否启用 动态加载插件
  const plugins = VITE_LOCAL_HTTPS ? [...defaultPlugins, basicSsl()] : [...defaultPlugins];
  return defineConfig({
    base: VITE_BASE,
    plugins,
    envDir: resolve("./env"),
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url))
      },
      extensions: [".js", ".ts", ".tsx", ".jsx"]
    },
    css: {
      preprocessorOptions: {
        less: {
          additionalData: `@import "@/styles/index.less";`
        }
      }
    },
    server: {
      port: VITE_PROT,
      https: VITE_LOCAL_HTTPS,
      open: VITE_OPEN,
      proxy: proxy(env)
    }
  });
};
